<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/7
 * Time: 16:27
 */

namespace app\sports_admin\model;


use think\Model;
use think\Request;

class Story extends Model
{
    protected $table = "story";
    protected $pk    = "id";
    protected $autoWriteTimestamp = true;

    /**
     * @param $page  页数
     * @param $rows 行数
     * @param $sort 排序字段
     * @param $sortOrder 升降序
     * @param $searchName 关键字
     */
    public static function storyList($page,$rows,$sort,$sortOrder,$searchName)
    {
        $adminList = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('title','like',"%{$searchName}%");
            }})
            ->page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")->select();

        $adminCount = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('title','like',"%{$searchName}%");
            }})
            ->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }
    //优秀教师事迹详情
    public static function storyMessage()
    {
        $id = Request::instance()->only('id');
        return self::where('id',$id['id'])->find();
    }
    //删除优秀教师事迹
    public static function storyDel()
    {
        $id = Request::instance()->only('id');
        return self::where("id",$id['id'])->delete();
    }

    /**
     * 添加优秀教师事迹
     * @return $this
     */
    public static function storyAdd()
    {
        $data = Request::instance()->only("content,title");
        $content = htmlspecialchars($data['content']);
        return self::create(["content"=>$content,"title"=>$data['title']]);
    }
    /**
     * 优秀教师事迹详情
     */
    public static function storyDetail()
    {
        $id = Request::instance()->only('id');
        return self::where('id',$id['id'])->find();
    }
    /**
     * 优秀教师事迹 更新内容
     */
    public static function storyUpdate()
    {
        $data = Request::instance()->only('id,title,content');
        $content = htmlspecialchars($data['content']);
        return self::where('id',$data['id'])->update(['content'=>$content,'title'=>$data['title']]);
    }
}